package CutLoad.components.container.viewer
{
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	import CutLoad.cookies.RegPointOperation;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.greensock.plugins.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 将一个Sprite对像放入这个容器中，它具有如下功能：缩小或放大尺寸，通过鼠标拖动。 
	 * @author feifei
	 * 
	 */	
	public class ScaleXYAndDrag extends Base
	{
		private var target:Sprite;
		private var viewWidth:Number,viewHeight:Number;
		private var spMask:Sprite;
		private var isMouseDown:Boolean=false;
		private var RPO:RegPointOperation;
		private var isPlay:Boolean=false;
		public function ScaleXYAndDrag(target:Sprite,viewWidth:Number,viewHeight:Number)
		{
			if(target.parent!=null) throw new Error("target.parent!=null");
			this.target=target;
			target.buttonMode=true;
			this.viewWidth=viewWidth;
			this.viewHeight=viewHeight;
			//
			spMask=Painter.getRect(viewWidth,viewHeight,0xff0000,.5);
			this.addChild(spMask);
			this.addChild(target);
			target.mask=spMask;
			//
			this.addEventListener(Event.ADDED_TO_STAGE,addStage);
			this.addEventListener(Event.ENTER_FRAME,addStage);
			//
			RPO=new RegPointOperation(this.target);
		}
		//偏差
		private var target_mouseX:Number,target_mouseY:Number;
		//目标要移动坐标
		private var moveToHereX:Number,moveToHereY:Number;
		//
		private var moveTweenLite:TweenLite;
		private function addStage(e:Event):void{
			switch(e.type){
				case Event.ADDED_TO_STAGE:
					target.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent);
					stage.addEventListener(MouseEvent.MOUSE_UP,mouseEvent);
					break;
				case Event.ENTER_FRAME:
					if(!isPlay) return;
					//if(this.mouseX<0||this.mouseX>this.viewWidth||this.mouseY<0||this.mouseY>this.viewHeight) return;
					var xx:Number
					var yy:Number
					if(isMouseDown){
						xx=mouseX-target_mouseX;
						yy=mouseY-target_mouseY;
					}else{
						xx=moveToHereX-target_mouseX;
						yy=moveToHereY-target_mouseY;
					}
					moveTweenLite=TweenLite.to(target, 1, {x:xx, y:yy, ease:Cubic.easeOut});
					break;
			}
		}
		private function moveTweenLite_kill():void{
			if(moveTweenLite!=null) moveTweenLite.kill();
		}
		private function mouseEvent(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					isPlay=true;
					isMouseDown=true;
					target_mouseX=this.mouseX-target.x;
					target_mouseY=this.mouseY-target.y;
					break;
				case MouseEvent.MOUSE_UP:
					isMouseDown=false;
					moveToHereX=this.mouseX;
					moveToHereY=this.mouseY;
					break;
			}
		}
		/**
		 * 显示边框 
		 * @param thickness
		 * @param color
		 * @param alpha
		 * 
		 */		
		public function showBorder(thickness:Number=2,color:Number=0x0000ff,alpha:Number=1):void{
			this.graphics.lineStyle(thickness,color,alpha);
			this.graphics.drawRect(0,0,this.getShowWidth(),this.getShowHeight());
		}
		/**
		 * 清除边框 
		 * 
		 */		
		public function hideBorder():void{
			this.graphics.clear();
		}
		public function zoom(speed:Number):void{
			moveTweenLite_kill();
			isPlay=false;
			RPO.setRegPointCenter();
			RPO.addScaleXY(speed,speed);
		}
		/**
		 * 移动显示对象的坐标 
		 * @param xx
		 * @param yy
		 * 
		 */		
		public function moveTarget(xx:Number,yy:Number):void{
			isPlay=true;
			moveTweenLite_kill();
			target_mouseX=0;
			target_mouseY=0;
			moveToHereX=xx;
			moveToHereY=yy;
		}
		/**
		 *  移动显示对象的到容器的中间
		 * 
		 */		
		public function moveTargetCenter():void{
			var xx:Number=target_mouseX;
			xx=(viewWidth-this.target.width)/2
			if(viewWidth>this.target.width){
				
			}else{
				
			}
			var yy:Number=target_mouseY;
			yy=(viewHeight-this.target.height)/2;
			if(viewHeight>this.target.height){
				
			}else{
				
			}
			moveTarget(xx,yy);
		}
		override public function getShowWidth():Number{
			return this.viewWidth;
		}
		override public function getShowHeight():Number{
			return this.viewHeight;	
		}
		override public function killMe():void{
			if(spMask!=null) this.removeChild(spMask);
			if(target!=null) this.removeChild(target);
			if(this.hasEventListener(Event.ADDED_TO_STAGE)) this.removeEventListener(Event.ADDED_TO_STAGE,addStage);
			if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,addStage);
			if(target.hasEventListener(MouseEvent.MOUSE_DOWN)) target.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEvent);
			if(stage.hasEventListener(MouseEvent.MOUSE_UP)) stage.removeEventListener(MouseEvent.MOUSE_UP,mouseEvent);
			super.killMe();
		}
	}
}